/**
 * 定义历史数据审核模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.WasteGas.reportHistory.reportDataAuditDetailManager', ['water.apps.common.report.reportBase','water.entity.monitorData']);
	
	module.controller('reportDataAuditDetailManagerController',function($rootScope,$scope,$controller,$dateUtil,$wasteGasMonitorDataService,$state,$stateParams){

		var control = $controller("commonReportAuditController",{$scope:$scope});
		
		$scope.dataAPIName = "minuteNPListGetter";
		$scope.dataAuditAPIName = "minuteAuditNPListGetter";
		$scope.dataExportAPIName = "exportMinExcel";
		

		$scope.curRowEdit={};
		$scope.parmList=[];
		/**
		 * 数据项
		 */
		$scope.dataType=[];
		/**
		 * 数据主键集合
		 */
		$scope.keys = [];
		/**
		 * 数据标识
		 */
		$scope.dataFlag=",";
		/**
		 * 数据分类
		 */
		$scope.dataClassify="1";
		/**
		 * 数据状态
		 */
		$scope.dataState="0";
		/**
		 * 开始日期
		 */
		$scope.startDate;
		/**
		 * 结束日期
		 */
		$scope.endDate;
		/**
		 * 开始小时
		 */
		$scope.startHour;
		/**
		 * 结束小时
		 */
		$scope.endHour;
		/**
		 * 数据标识集合
		 */
		$scope.dataFlags = [ {"k":"N","v":"N(正常)"},
		                     {"k":"O","v":"O(超标)"},{"k":"A","v":"A(异常)"}
		                     ,{"k":"F","v":"F(停运)"},{"k":"Bh","v":"Bh(开炉半小时)"},{"k":"P","v":"P(停电)"},
		                     {"k":"T","v":"T(仪器故障)"}];
		
		
		/**
		 * 审核按钮事件，用于切换页面
		 */
		$scope.onReturnDataAudit = function(){
			var data = {};
			data.startDate = $scope.startDate;
			data.endDate = $scope.endDate;
			data.startHour = $scope.startHour;
			data.endHour = $scope.endHour;
			$state.go("Report.history.dataAudit",{curRow:data});
		}
		
		/**
		 * 数据状态切换按钮
		 */
		$scope.onDataStateChange = function(){
			var queryObject = $scope.getQueryObject();
			if($scope.dataState==1)  //审核后数据
			{
				$scope.getAuditReport(queryObject);
			}else{ 	 	 //原始数据
				$scope.getReport(queryObject);
			}

		}
		
		/**
		 * 数据标识保存按钮
		 */
		$scope.onSaveClick = function(){
			var data = $scope.data.data;
			var json = ObjectArrayToJsonString(data);
			var queryObject = $scope.getQueryObject();
			queryObject.data = json;
			//保存审核数据,成功后跳转到审核完成界面
			$wasteGasMonitorDataService.saveAuditData(queryObject,function(data){
				$scope.dataState="1";
				$scope.onDataStateChange();
				$.messager.alert("保存成功","保存成功",'info');
			});
		}
		
		
		/**
		 * 数据审核修改页面
		 */
		$scope.onDataRowEdit = function(curRow,index){
			$scope.curRowEdit = curRow;
//			angular.copy(curRow, $scope.curRowEdit); 
//			$scope.curRowEdit.index = index;
			$scope.dataAuditDialog.open();
		}
		
		/**
		 * 关闭数据审核修改页面
		 */
//		$scope.onCloseEditClick = function(){
//			$scope.data.data[$scope.curRowEdit.index] = {};
//			angular.copy($scope.curRowEdit,$scope.data.data[$scope.curRowEdit.index]); 
//			$scope.dataAuditDialog.close();
//		}
//		
		//对象数组转换成JSON字符串
		function ObjectArrayToJsonString(objArray) {  
		    var objCount = arrayCount(objArray);  
		    if (objCount > 0) {  
		        var myJson = "[";  
		        for (var i = 0; i < objCount; i++) {  
		            myJson += JSON.stringify(objArray[i])+",";  
		        }      
		        myJson = myJson.substring(0,myJson.length - 1) + "]";  
		        return myJson;  
		     }  
		    return null;  
		}  
		function arrayCount(myArray) {  
		    var arrayType = typeof myArray;  
		    if (arrayType = "string") {  
		        return myArray.length;  
		    }  
		    else if (arrayType = "object") {  
		        var n = 0;  
		        for (var i in myArray) {  
		            n++;  
		        }  
		        return n;  
		    }  
		    return false;  
		}  
		
		/**
		 * 监听左侧列表选择
		 */
		$scope.oNdataPramChanged = $scope.$on('dataPramChanged', function(event, station) {
			$scope.onStationSelectedBeforeQuery(station);
			//初始化
			_init();
			pollFreshLag=true;			
	    });
		
		/**
		 * 站点改变后，更新构建报表的参数，比如表头，数据项等
		 */
		$scope.onStationSelectedBeforeQuery = function(station){
			$scope.selectedModel = station;
			$scope.parmList = station.parmToCheckList;
			var parmListNew=[];
			$scope.dataType=[];
			for(var i=0;i<$scope.parmList.length;i++){
				var parm=$scope.parmList[i];
				if(parm.name=="01"||parm.name=="02"||parm.name=="03"||parm.name=="04"||parm.name=="06"||parm.name=="08") {
					parm.colspan=2;
					$scope.dataType.push("实测值");
					$scope.dataType.push("折算值");
				}
				else{
					parm.colspan=1;
					$scope.dataType.push("实测值");
				}
				parmListNew.push(parm);
			}
			$scope.parmList= parmListNew;
			
			$scope.keys = [];
			for(var i=0;i<$scope.parmList.length;i++){
				var obj = $scope.parmList[i];
				if(obj.name=="01"||obj.name=="02"||obj.name=="03"||obj.name=="04"||obj.name=="06"||obj.name=="08") {
					$scope.keys.push("AVG_"+obj.name);
					$scope.keys.push("ZS_AVG_"+obj.name);
				}
				else{
					$scope.keys.push("AVG_"+obj.name);
				}
			}
		};
				
		/**
		 * 获取查询参数
		 * forExcel 是否用于导出
		 */
		$scope.getQueryObject = function(forExcel){
			var startTime =$scope.startDate + " " + $scope.startHour + ":00:00";
			var endTime = $scope.endDate + " " + $scope.endHour + ":55:00";
			var parmsStr = $scope.selectedModel.parmToChecksStr;
			var queryObject = {
					"MN":$scope.selectedModel.MN,
					"PARMS":parmsStr,
					"START":startTime,
					"END":endTime,
					"EXPORT":forExcel,
				};
			return queryObject;
		}
		
		/**
		 * 初始化
		 */
		function _init(){
			$scope.detailData=$stateParams.curRow;
			if($stateParams.curRow!=null)
			{
				$scope.startDate = $stateParams.curRow.startDate;
				$scope.endDate = $stateParams.curRow.endDate;
				$scope.startHour = $stateParams.curRow.startHour;
				$scope.endHour = $stateParams.curRow.endHour;
				$scope.dataState = $stateParams.curRow.dataState;
			}
			//获取点位该时间段内的数据是否已经审核过
			var queryObject = $scope.getQueryObject();
			$wasteGasMonitorDataService.queryDataState(queryObject,function(data){
				$scope.dataState=data.data;
    			$scope.onDataStateChange();
			});	
		}
	});
}(window, angular));	